package com.gem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gem.entity.User;
import com.gem.service.UserService;
import com.gem.util.ZhenziUtil;
import com.zhenzi.sms.ZhenziSmsClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @author 刘聪
 * Created  on 2021/3/15
 */
@Controller
@RequestMapping("/safety")
public class SaftyController {
    private ZhenziSmsClient client = new ZhenziSmsClient(
            ZhenziUtil.apiUrl, ZhenziUtil.appId, ZhenziUtil.appSecret);

    @Autowired
    private UserService userService;

    /**
     * 绑定手机
     *
     * @return
     */
    @GetMapping("/bindphone")
    public String bindPhone() {
        return "/user/bindphone";
    }

    @PostMapping("/oldPhone")
    @ResponseBody
    public String oldPhone(String phone, HttpSession session) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("number", phone);
        params.put("templateId", "4032");
        String[] templateParams = new String[2];
        Integer r = new Random().nextInt(90000) + 10000;
        String code = r + "";
        templateParams[0] = code;
        templateParams[1] = "60秒";
        params.put("templateParams", templateParams);
        session.setAttribute("oldPhone", code);
        String result = client.send(params);
        return result;
    }

    @PostMapping("/newPhone")
    @ResponseBody
    public String newPhone(String phone, HttpSession session) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("number", phone);
        params.put("templateId", "4032");
        String[] templateParams = new String[2];
        Integer r = new Random().nextInt(90000) + 10000;
        String code = r + "";
        templateParams[0] = code;
        templateParams[1] = "60秒";
        params.put("templateParams", templateParams);
        session.setAttribute("newPhone", code);
        String result = client.send(params);
        return result;
    }

    @PostMapping("/changPhone")
    @ResponseBody
    public Integer changPhone(String oldPhone, String newPhone, String oldCode, String newCode, HttpSession session) {
        Object oldPhoneCode = session.getAttribute("oldPhone");
        Object newPhoneCode = session.getAttribute("newPhone");
        if (oldCode == oldPhoneCode && newCode == newPhoneCode) {
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("phone", oldPhone);
            User user = userService.getOne(wrapper);
            user.setPhone(newPhone);
            userService.updateById(user);
            return 1;
        }
//        System.out.println(sendCode + "--" + code);
        return 0;
    }

    /**
     * 更改密码
     * @return
     */
    @GetMapping("/password")
    public String password() {
        return "/user/password";
    }

    @PostMapping("/password")
    @ResponseBody
    public Integer changePassword(String oldPassword, String newPassword, String confirmPassword, HttpSession session) {
        User user = (User) session.getAttribute("User");
        String password = user.getPassword();
        if (oldPassword.equals(newPassword)) {
            /*旧密码与新密码一致*/
            return 0;
        } else if (!newPassword.equals(confirmPassword)) {
            /*新密码不一致*/
            return 1;
        } else if (!password.equals(oldPassword)) {
            /*旧密码输入错误*/
            return 2;
        } else {
            user.setPassword(newPassword);
            userService.updateById(user);
            return 3;
        }
    }

    /**
     * 更改邮箱
     * @return
     */
    @GetMapping("/email")
    public String email() {
        return "/user/email";
    }

    @PostMapping("/email")
    public String checkemail(String email,HttpSession session) {
        User user = (User) session.getAttribute("User");
        user.setEmail(email);
        userService.updateById(user);
        return "/user/index";
    }
}
